package haoyc.utils;

import haoyc.entity.SimpleKeyEvent;
import haoyc.entity.SimpleMouseEvent;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author Memory
 * @version 1.0
 * @date 2021/11/22
 */

@Slf4j
public class SwingClient {

    private static JFrame jf;
    private static JLabel jl;
    private static ChannelHandlerContext ctx;

    public static JFrame getJFrame(ChannelHandlerContext ctx, int height, int weight) {
        if (null == jf) {
            synchronized (SwingClient.class) {
                if (null == jf) {
                    jf = new JFrame("控制器");
                    jf.setSize(weight, height + 50);
                    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    jf.setVisible(true);
                    SwingClient.ctx = ctx;
                    new SwingClient().setListener(jf);
                }
            }
        }
        return jf;
    }

    public static JLabel getJLabel() {
        if (null == jl) {
            synchronized (SwingClient.class) {
                if (null == jl) {
                    jl = new JLabel();
                }
            }
        }
        return jl;
    }

    public void setListener(JFrame frame) {
        //panel设置监听器
        frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                sendKeyEventObject(e);
            }

            @Override
            public void keyReleased(KeyEvent e) {
                sendKeyEventObject(e);
            }

            @Override
            public void keyTyped(KeyEvent e) {
                sendKeyEventObject(e);
            }
        });
        frame.addMouseWheelListener(e -> sendMouseEventObject(e));
        frame.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                sendMouseEventObject(e);
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                sendMouseEventObject(e);
            }
        });
        frame.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                sendMouseEventObject(e);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                sendMouseEventObject(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                sendMouseEventObject(e);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                sendMouseEventObject(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                sendMouseEventObject(e);
            }
        });
    }


    private void sendMouseEventObject(MouseEvent event) {
        ctx.writeAndFlush(new SimpleMouseEvent(event.getID(), event.getX(), event.getY(), event.getButton()));
    }

    private void sendKeyEventObject(KeyEvent event) {
        ctx.writeAndFlush(new SimpleKeyEvent(event.getID(), event.getKeyCode()));
    }

    public static void main(String[] args) {
        try {

            String imgPath = "E:\\Document\\irresistibly\\test\\" + 6 + ".jpg";
            BufferedImage image = ImageIO.read(new FileInputStream(imgPath));
            JFrame jFrame = SwingClient.getJFrame(null, image.getHeight(), image.getWidth());
            JLabel background = new JLabel();
            for (int i = 0; i < 5; i++) {
                Thread.sleep(1000);
                System.out.println(i);
                imgPath = "E:\\Document\\irresistibly\\test\\" + (i + 1) + ".jpg";
                image = ImageIO.read(new FileInputStream(imgPath));

                jFrame.add(background);
                background.setIcon(new ImageIcon(image));
                jFrame.repaint();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
